<%-- 
    Document   : Procesar_Dar_De_Alta
    Created on : 21-nov-2013, 13:33:26
    Author     : Javi
--%>

<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Logger"%>
<%@page import="java.util.logging.Level"%>
<%@page import="java.util.logging.Level"%>
<%@page import="GestorBDatos.PruebaGB"%>
<%@page import="GestorBDatos.exceptions.PreexistingEntityException"%>
<%@page import="GestorBDatos.UsuarioJpaController"%>
<%@page import="GestorBDatos.Usuario"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String nombre = request.getParameter("nombre");
            String apellidos = request.getParameter("apellidos_usuario");
            String login = request.getParameter("nombre_usuario");
            String pass = request.getParameter("password_usuario");
            String control_pass = request.getParameter("control_password_usuario");
            String correo = request.getParameter("correo_usuario");
            String ip="nada";
            
            if (request.getHeader("HTTP_X_FORWARDED_FOR") == null) {

                ip = request.getRemoteAddr();

            } else {

                ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            }


            
            //String ip = request.getRemoteAddr();
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("Seguridad_InformaticaPU");
            UsuarioJpaController userController = new UsuarioJpaController(emf);
            String original = (String) session.getAttribute("captcha");
            String sec = request.getParameter("seccode");

            if (userController.findUsuario(login) != null) {
                response.sendRedirect("Pantalla_Alta.jsp?error=1");
            } else if (!pass.equals(control_pass)) {
                response.sendRedirect("Pantalla_Alta.jsp?error=2");
            } else if (nombre == "" || apellidos == "" || login == ""
                    || pass == "" || control_pass == "" || correo == "") {
                response.sendRedirect("Pantalla_Alta.jsp?error=3");
            } else if (!original.equals(sec)) {
                response.sendRedirect("Pantalla_Alta.jsp?error=4");
            } else if (!correo.contains("@")) {
                response.sendRedirect("Pantalla_Alta.jsp?error=5");
            } else if (nombre.length() > 20 || apellidos.length() > 20 || login.length() > 20
                    || pass.length() > 20 || control_pass.length() > 20 || correo.length() > 30) {
                response.sendRedirect("Pantalla_Alta.jsp?error=6");
            } else if (pass.length() < 6) {
                response.sendRedirect("Pantalla_Alta.jsp?error=7");
            } else {
                GregorianCalendar horaActual = new GregorianCalendar();
                Usuario u = new Usuario(login, nombre, apellidos, correo, pass, ip, UsuarioJpaController.TipoUsuario.user.name(), "0", horaActual.getTime());
                try {
                    userController.create(u);
                } catch (PreexistingEntityException ex) {
                    Logger.getLogger(PruebaGB.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(PruebaGB.class.getName()).log(Level.SEVERE, null, ex);
                }
                response.sendRedirect("Pantalla_Registrado.jsp");
            }

        %>
    </body>

</html>
